package lq.auth.studyauth.manager;

import lq.auth.studyauth.mapper.MenuMapper;
import lq.auth.studyauth.mapper.UserMapper;
import lq.common.entity.system.Menu;
import lq.common.entity.system.SystemUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: cloud-study
 * @description:
 * @author: LQ
 * @create: 2021-01-26 13:57
 */
@Service
public class UserManager {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MenuMapper menuMapper;

    /**
     * 根据用户名获取用户
     *
     * @param username
     * @return
     */
    public SystemUser findByName(String username) {
        return userMapper.findByName(username);
    }

    /**
     * 获取用户权限
     *
     * @param username
     * @return
     */
    public String findUserPermissions(String username) {
        /*List<Menu> userPermissions = menuMapper.findUserPermissions(username);

        List<String> perms = new ArrayList<>();
        for (Menu m: userPermissions){
            perms.add(m.getPerms());
        }
        return StringUtils.join(perms, ",");*/

        List<Menu> userPermissions = menuMapper.findUserPermissions(username);
        return userPermissions.stream().map(Menu::getPerms).collect(Collectors.joining(","));
    }
}